package com.hsd.whb.utils;

import com.hsd.whb.enums.ResultEnum;
import com.hsd.whb.exception.HwbException;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FileUtil {

    /*
     * 获取文件后缀
     */
    public static String getFileExt(String fileName) {
        int dotIndex = fileName.lastIndexOf(".");
        String fileExt = fileName.substring(dotIndex + 1);
        return fileExt;
    }

    /*
     * 获取文件名
     */
    public static String getFileName(String file) {
        int dotIndex = file.lastIndexOf("/");
        String fileName = file.substring(dotIndex + 1);
        return fileName;
    }

    /**
     * 删除文件夹里面的所有文件
     *
     * @param path 文件夹路径 如 c:/fqf
     */
    public static void delAllFile(String path) {
        File file = new File(path);
        if (!file.exists()) {
            return;
        }
        if (!file.isDirectory()) {
            return;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
                delFolder(path + "/" + tempList[i]);// 再删除空文件夹
            }
        }
    }

    /**
     * 删除文件夹
     *
     * @param folderPath 文件夹路径及名称 如c:/fqf
     */
    public static void delFolder(String folderPath) {
        try {
            delAllFile(folderPath); // 删除完里面所有内容
            String filePath = folderPath;
            filePath = filePath.toString();
            File myFilePath = new File(filePath);
            myFilePath.delete(); // 删除空文件夹

        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    /**
     * 复制单个文件
     *
     * @param oldPath 源文件路径
     * @param newPath 复制后路径
     * @return 文件大小
     */
    public static int copyFile(String oldPath, String newPath) {
        try {
            int bytesum = 0;
            int byteread = 0;
            File oldfile = new File(oldPath);
            if (oldfile.exists()) {
                InputStream inStream = new FileInputStream(oldPath); // 读入原文件
                FileOutputStream fs = new FileOutputStream(newPath);
                byte[] buffer = new byte[1444];
                int length;
                while ((byteread = inStream.read(buffer)) != -1) {
                    bytesum += byteread; // 字节数 文件大小
                    System.out.println(bytesum);
                    fs.write(buffer, 0, byteread);
                }
                inStream.close();
                fs.close();
            }
            return bytesum;
        } catch (Exception e) {
            System.out.println("复制单个文件操作出错");
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 复制文件流到新的文件
     *
     * @param inStream 文件流
     * @param file     新文件
     * @return 是否复制成功
     */
    public static boolean copyInputStreamToFile(final InputStream inStream, File file) throws IOException {
        int bytesum = 0;
        int byteread = 0;
        byte[] buffer = new byte[1024];
        FileOutputStream fs = new FileOutputStream(file);
        while ((byteread = inStream.read(buffer)) != -1) {
            bytesum += byteread; // 字节数 文件大小
            fs.write(buffer, 0, byteread);
        }
        inStream.close();
        fs.close();
        return true;
    }

    /**
     * 删除指定路径下的文件
     *
     * @param filePathAndName 文件路径
     */
    public static void delFile(String filePathAndName) {
        try {
            String filePath = filePathAndName;
            filePath = filePath.toString();
            File myDelFile = new File(filePath);
            myDelFile.delete();

        } catch (Exception e) {
            System.out.println("删除文件操作出错");
            e.printStackTrace();
        }
    }


    public static String copyToFile(MultipartFile multipartFile, String parentUrl) {
        try {
            File parentFile = new File(parentUrl);
            if (!parentFile.exists()) parentFile.mkdirs();
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss.");
            String filename = format.format(new Date()) + getFileExt(multipartFile.getOriginalFilename());
            File file = new File(parentFile, filename);
            multipartFile.transferTo(file);
            return filename;
        } catch (IOException e) {
            e.printStackTrace();
            throw new HwbException(ResultEnum.IMAGE_UPLOAD_ERROR);
        }
    }

    public static void zipFile(String parentFilePath, File zipFile) throws IOException {
        File parentFile = new File(parentFilePath);
        File[] fileList = parentFile.listFiles();
        FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
        ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
        for (File file : fileList) {
            if (!FileUtil.getFileExt(file.getName()).equals("zip")) {
                zipFile(file, zipOutputStream);
            }
        }
        zipOutputStream.close();
        fileOutputStream.close();
    }

    public static void zipFile(File[] fileList, File zipFile) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
        ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
        for (File file : fileList) {
            if (!FileUtil.getFileExt(file.getName()).equals("zip")) {
                zipFile(file, zipOutputStream);
            }
        }
        zipOutputStream.close();
        fileOutputStream.close();
    }

    private static void zipFile(File file, ZipOutputStream zipOutputStream) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(file);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
        byte[] buffer = new byte[10 * 1024 * 1024];
        int length;
        while ((length = bufferedInputStream.read(buffer, 0, buffer.length)) != -1) {
            zipOutputStream.write(buffer, 0, length);
        }
        zipOutputStream.closeEntry();
        bufferedInputStream.close();
        fileInputStream.close();
    }

}
